#!/bin/bash
#二进制安装mysql5.7
. ./var
. ./Tmpdir
mysql_bin(){
    Mysql_Version="mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz"
    TAR_Mysql="mysql-5.7.28-linux-glibc2.12-x86_64"
    basedir="/usr/local/mysql"
    datadir="/MysqlData"

    #调用Tmpdir函数
    Tmpdir
    cd $dir && wget -V &> /dev/null || yum -y install wget
    [ -d /usr/local/mysql ] && echoRed "检测到/usr/local下已安装mysql，故而退出！" && rm -rf $dir && exit 1
    
    wget $ip/${Mysql_Version} && mqnu=`cat /etc/passwd | grep mysql |wc -l`
    #cp /root/${Mysql_Version} . && mqnu=`cat /etc/passwd | grep mysql |wc -l`
    #rsync -azv /root/${Mysql_Version} . && mqnu=`cat /etc/passwd | grep mysql |wc -l`
    if [ $mqnu -ne 1 ];then
        echoRed "mysql用户不存在，新建用户，然后在进行下一步操作……" && groupadd mysql && useradd -r -g mysql -c "`date +%Y/%m/%d`, install mysql" -s /bin/false mysql
#	useradd -g mysql -s /sbin/nologin mysql
    else
        echoRed "mysql已经存在，进行下一步操作……"
    fi


    #安装依赖包
    yum install -y libaio
    #解压mysql二进制软件包，并修改权限
    tar xf ${Mysql_Version} -C /usr/local/
    [ -d /usr/local/${TAR_Mysql} ] && ln -sv /usr/local/${TAR_Mysql} /usr/local/mysql
    [ ! -d /usr/local/mysql ] && echoRed "mysql未链接成功，即将删除临时目录" && rm -rf $dir && exit 29
    chown -R mysql:mysql /usr/local/${TAR_Mysql} /usr/local/mysql

    #检查是否存在mysql的数据目录，没有就创建，然后修改数据目录的所属组、主
    [ ! -d ${datadir} ] && mkdir -p ${datadir} && chown -R mysql.mysql ${datadir}
    echoGreen "初始化数据库，注册为服务……" && cd ${basedir}/bin  && ./mysqld --initialize --user=mysql --basedir=${basedir} --datadir=${datadir}
    if [ $? = 0 ];then
        echoGreen "数据库初始化成功，下一步配置mysql的主配置文件my.cnf"
    else
       echoGreen "初始化数据库，注册为服务……" && cd /usr/local/${TAR_Mysql}/bin  && ./mysqld --initialize --user=mysql --basedir=${basedir} --datadir=${datadir}
           if [ $? = 0 ];then
	        echoGreen "数据库初始化成功,下一步配置mysql的主配置文件my.cnf"
	   else
                echoRed "数据库初始化失败，请检查，即将退出"
                rm -rf $dir
                exit 23
           fi
    fi
    


    #修改配置文件my.cnf
    [ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4


[mysqld]
datadir=${datadir}
basedir = ${basedir}
socket=/tmp/mysql.sock
pid-file = /tmp/mysql.pid
user = mysql
bind-address = 0.0.0.0
#开启binlog的前提是需要开启server-id,否则mysql启动失败，报找不到pid文件
server-id = 1
#开启binlog
log-bin = ${datadir}/mysql-bin
#log-bin = mysql-bin
#skip-grant-tables

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=${datadir}/mysql-error.log
pid-file=/tmp/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
EOF
    [ -d /etc/my.cnf.d/ ] || mkdir /etc/my.cnf.d

    ${basedir}/bin/mysql -V &> /dev/null 
    if [ $? = 0 ];then
        echoGreen "已完成安装，可尽情享用！"
    else
        echoYellow "可能安装有问题，请检查！，即将删除临时目录并且退出安装脚本" 
        rm -rf $dir
        exit 20
    fi
 
    #配置启动脚本，并修改启动脚本中的basedir和datadir的路径,最后加入开机启动
    [ -d ${basedir}/support-files ] && cd ${basedir}/support-files && cp mysql.server /etc/rc.d/init.d/mysql && chmod +x /etc/rc.d/init.d/mysql
    #注意：这里不能用斜线来当分隔符，可以用@或者#或者!或者|这四个来做分隔符，否则会报错
    sed -i "s@^basedir=.*@basedir=${basedir}@"  /etc/rc.d/init.d/mysql
    sed -i "s#^datadir=.*#datadir=${datadir}#"  /etc/rc.d/init.d/mysql
    chkconfig --add mysql &&  chkconfig mysql on

    #添加环境变量
    echo "export PATH=${basedir}/bin:\$PATH" >> /etc/profile && source /etc/profile
    #启动服务器
    service mysql start || /etc/init.d/mysql restart

    S && rm -rf $dir
    echoRed "由于是安装的mysql5.7，因此需要进入数据库修改密码，然后在重启数据库，最后重新登录数据库才能操作……"
    echoYellow "请手动配置数据库,输入如下命令"
    echoYellow 'mysql -uroot -p'
    echoYellow "初始密码是初始化数据库产生的临时密码"
    echoYellow 'set password = password("666666");'
    echoYellow "flush privileges;"
}
